package java10;
import java.sql.*;
import java.util.Scanner;
//登录方法，使用PreparedStatement实现
public class JdbcTest07 {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            java10.JdbcTest07 jc = new java10.JdbcTest07();
            boolean flag = jc.login(username,password);
            if(flag){
                System.out.println("登录成功！");
            }else{
                System.out.println("用户名或密码错误！");
            }
        }
        public boolean login(String username,String password){
            if(username == null || password == null){
                return false;
            }
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                conn = JDBCUtils.getConnection();
                //定义sql
                String sql = "select * from user where username = ? and password = ? ";
                //获取执行sql的对象
                pstmt = conn.prepareStatement(sql);
                //给问好赋值
                pstmt.setString(1,username);
                pstmt.setString(2,password);
                //执行查询,不需要传递sql
                rs = pstmt.executeQuery();
                return rs.next();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                JDBCUtils.close(rs,pstmt,conn);
            }
            return false;
        }
}
